﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 答题面板
/// </summary>
public class AnswerPanel : BasePanel
{
    public Text index_text;
    public Button close_btn;
    public Text topic_text;
    public Text option1_text;
    public Text option2_text;
    public Button option1_btn;
    public Button option2_btn;
    public Image flag_true;
    public Image flag_false;

    private void Awake()
    {
        close_btn.onClick.AddListener(OnClickClose);
        option1_btn.onClick.AddListener(OnClickOption1);
        option2_btn.onClick.AddListener(OnClickOption2);
    }
    private void OnClickClose()
    {
        AudioManager.Instance.PlayClick();
        UIManager.Instance.Pop();
    }

    public override void OnActive()
    {
        Reflash();
    }

    private Question GetCurentQuestion()
    {
        return GameManager.Instance.GetCurrentQuestion();
    }

    private int GetCurrentQuestionIndex()
    {
        return GameManager.Instance.GetCurrentQuestionIndex() + 1;
    }

    private void OnClickOption1()
    {
        if (clickOption1 || clickOption2)
        {
            return;
        }
        clickOption1 = true;
        bool result = GameManager.Instance.SubmitAnswer(0);
        if (result)
        {
            AudioManager.Instance.PlaySuccess();
            flag_true.transform.position = option1_btn.transform.position;
            flag_true.gameObject.SetActive(true);
        }
        else
        {
            AudioManager.Instance.PlayFail();
            flag_false.transform.position = option1_btn.transform.position;
            flag_false.gameObject.SetActive(true);
        }
        Invoke("Next",0.5f);
    }

    private void OnClickOption2()
    {
        if (clickOption1 || clickOption2)
        {
            return;
        }
        clickOption2 = true;
        bool result = GameManager.Instance.SubmitAnswer(1);
        if (result)
        {
            AudioManager.Instance.PlaySuccess();
            flag_true.transform.position = option2_btn.transform.position;
            flag_true.gameObject.SetActive(true);
        }
        else
        {
            AudioManager.Instance.PlayFail();
            flag_false.transform.position = option2_btn.transform.position;
            flag_false.gameObject.SetActive(true);
        }
        Invoke("Next", 0.5f);
    }

    private void Next()
    {
        if(GameManager.Instance.GetGameState() == GameState.Fail)
        {
            UIManager.Instance.Push("FailPanel");
        }else if (GameManager.Instance.GetGameState() == GameState.Success)
        {
            UIManager.Instance.Push("SuccessPanel");
        }
        else
        {
            Reflash();
        }
    }


    private bool clickOption1;
    private bool clickOption2;
    private void Reflash()
    {
        Question question = GetCurentQuestion();
        int index = GetCurrentQuestionIndex();
        index_text.text = "第" + index.ToCN() + "题";
        topic_text.text = question.topic;
        option1_text.text = "A." + question.options[0].text;
        option2_text.text = "B." + question.options[1].text;
        flag_true.gameObject.SetActive(false);
        flag_false.gameObject.SetActive(false);
        clickOption1 = false;
        clickOption2 = false;
    }
}
